$(function() {
    let selPage = 1;

    function getAll(page) {
        selPage = page
        $.ajax({
            type: 'get',
            url: BigNew.comment_list,
            data: {
                page: page,
                perpage: 12
            },
            success: function(res) {
                console.log(res);
                if (res.code == 200) {
                    let htmlStr = template('tp-list', res.data);
                    $('tbody').html(htmlStr)
                    $('#pagination').twbsPagination({
                        totalPages: res.data.totalPage,
                        visiblePages: 12,
                        // 控制按钮的文字显示
                        first: '首页',
                        last: '尾页',
                        next: '下一页',
                        prev: '上一页',
                        // 关闭默认的初始点击事件
                        initiateStartPageClick: false,
                        //页码的点击事件
                        onPageClick: function(event, page) {
                            getAll(page)
                        }
                    });
                }
            }
        })
    }
    getAll(1);

    //封装函数
    function getQuest(target, dataId) {
        let url;
        if (target == '批准') {
            url = BigNew.comment_pass;
        } else if (target == '拒绝') {
            url = BigNew.comment_reject;
        } else {
            url = BigNew.comment_delete;
        }
        $.ajax({
            type: 'post',
            url: url,
            data: {
                id: dataId
            },
            success: function(res) {
                // console.log(res);
                if (res.code == 200) {
                    if ($('tbody tr').length > 1 || target == '批准' || target == '拒绝') {
                        getAll(selPage)
                    } else {
                        getAll(selPage - 1)
                    }
                }

            }

        })
    }
    //给三个按钮设置点击事件
    $('tbody').on('click', 'a', function() {
        let txt = $(this).text();
        // console.log(txt);
        //获取当前评论的id值
        let comId = $(this).attr('data-id');
        getQuest(txt, comId)

    })
})